﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebClass;

namespace TestMeausreValue
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestPreAndRe()
        {
            ExperimentsQueries exper = new ExperimentsQueries();


            //tp=0, fp=0, fn = 0 equal listQueryRank.Count = 0, goldenSetCount = 0
            List<int> listQueryRank = new List<int>();
            
            PrecisionRecallCoverage data = new PrecisionRecallCoverage();
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 0, data);
            
            Assert.AreEqual(0, data.PrecisionOutput.Result);
            Assert.AreEqual(0, data.RecallOutput.Result);

            //tp=100, fp=0, fn = 0  equal listQueryRank.Count = 100 and all items are '0', goldenSetCount = 100
            listQueryRank = new List<int>();
            for (int i = 0; i < 100; ++i)
            {
                listQueryRank.Add(0);//rank = 0 or rank = 1
            }
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 100, data);
            Assert.AreEqual(1, data.PrecisionOutput.Result);
            Assert.AreEqual(1, data.RecallOutput.Result);

            //tp=0, fp=100, fn = 0  equal listQueryRank.Count = 100 and all items are '2', goldenSetCount = 0
            listQueryRank = new List<int>();
            for (int i = 0; i < 100; ++i)
            {
                listQueryRank.Add(2);//rank = 2
            }
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 0, data);
            Assert.AreEqual(0, data.PrecisionOutput.Result);
            Assert.AreEqual(0, data.RecallOutput.Result);


            //tp=0, fp=0, fn = 100  equal listQueryRank.Count = 0, goldenSetCount = 100
            listQueryRank = new List<int>();
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 100, data);
            Assert.AreEqual(0, data.PrecisionOutput.Result);
            Assert.AreEqual(0, data.RecallOutput.Result);


            //tp=100, fp=100, fn = 0  equal listQueryRank.Count = 200 and half items are '0' and half items are '2', goldenSetCount = 100
            listQueryRank = new List<int>();
            for (int i = 0; i < 100; ++i)
            {
                listQueryRank.Add(0);//rank = 0 or rank = 1
            }
            for (int i = 0; i < 100; ++i)
            {
                listQueryRank.Add(2);//rank = 2
            }
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 100, data);
            Assert.AreEqual(0.5, data.PrecisionOutput.Result);
            Assert.AreEqual(1, data.RecallOutput.Result);


            //tp=100, fp=0, fn = 100  equal listQueryRank.Count = 100 and all items are '1', goldenSetCount = 200
            listQueryRank = new List<int>();
            for (int i = 0; i < 100; ++i)
            {
                listQueryRank.Add(1);//rank = 0 or 1
            }
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 200, data);
            Assert.AreEqual(1, data.PrecisionOutput.Result);
            Assert.AreEqual(0.5, data.RecallOutput.Result);


            //tp=0, fp=100, fn = 100  equal listQueryRank.Count = 100 and all items are '2', goldenSetCount = 0
            listQueryRank = new List<int>();
            for (int i = 0; i < 100; ++i)
            {
                listQueryRank.Add(2);//rank = 2
            }
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 100, data);
            Assert.AreEqual(0, data.PrecisionOutput.Result);
            Assert.AreEqual(0, data.RecallOutput.Result);

            //tp=100, fp=100, fn = 100  equal listQueryRank.Count = 200 and half items are '0' and half items are '2', goldenSetCount = 200
            listQueryRank = new List<int>();
            for (int i = 0; i < 100; ++i)
            {
                listQueryRank.Add(0);//rank = 0 or 1
            }
            for (int i = 0; i < 100; ++i)
            {
                listQueryRank.Add(2);//rank = 2
            }
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 200, data);
            Assert.AreEqual(0.5, data.PrecisionOutput.Result);
            Assert.AreEqual(0.5, data.RecallOutput.Result);


            //tp=30, fp=90, fn = 50  equal listQueryRank.Count = 110, goldenSetCount = 80
            listQueryRank = new List<int>();
            for (int i = 0; i < 30; ++i)
            {
                listQueryRank.Add(0);//rank = 0 or 1
            }
            for (int i = 0; i < 90; ++i)
            {
                listQueryRank.Add(2);//rank = 2
            }
            exper.Precision(listQueryRank, data);
            exper.Recall(listQueryRank, 80, data);
            Assert.AreEqual(0.25, data.PrecisionOutput.Result);
            Assert.AreEqual(0.375, data.RecallOutput.Result);


            /*Test coverage*/
            //HitTargetCount = 0,Total = 0,Coverage = 0
            List<int> listQueryPosition = new List<int>();
            exper.Coverage(listQueryPosition, data);
            Assert.AreEqual(0, data.CoverageOutput.Result);

            //HitTargetCount = 0,Total = 100,Coverage = 0;
            listQueryPosition = new List<int>();
            for(int i = 0;i < 100; ++i)
            {
                listQueryPosition.Add(-1);
            }
            exper.Coverage(listQueryPosition, data);
            Assert.AreEqual(0, data.CoverageOutput.Result);

            //HitTargetCount = 100,Total = 100,Coverage = 1;
            listQueryPosition = new List<int>();
            for (int i = 0; i < 100; ++i)
            {
                listQueryPosition.Add(0);
            }
            exper.Coverage(listQueryPosition, data);
            Assert.AreEqual(1, data.CoverageOutput.Result);
            
            //HitTargetCount = 50,Total = 100,Coverage = 0.5
            listQueryPosition = new List<int>();
            for (int i = 0; i < 50; ++i)
            {
                listQueryPosition.Add(1);
            }
            for (int i = 50; i < 100; ++i)
            {
                listQueryPosition.Add(-2);
            }
            exper.Coverage(listQueryPosition, data);
            Assert.AreEqual(0.5, data.CoverageOutput.Result);
        }
    }
}
